package com.wego.controller.admin;

import com.wego.bean.PageBean;
import com.wego.bean.ResultBean;
import com.wego.converter.PopularTextConverter;
import com.wego.domain.details.PopularTextDetails;
import com.wego.domain.dto.PopularTextDTO;
import com.wego.domain.entity.PopularText;
import com.wego.domain.query.PopularTextQuery;
import com.wego.domain.vo.PopularTextVO;
import com.wego.service.PopularTextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 热点(tb_popularText)表控制层
 *
 */
@RestController
@RequestMapping("/admin/popularText")
public class AdminPopularTextController {
    @Autowired
    private PopularTextService popularTextService;
    @Autowired
    private PopularTextConverter popularTextConverter;


    @PostMapping("/v1/page")
    public ResultBean<PageBean<PopularTextVO>> page(@RequestBody PopularTextQuery popularTextQuery) {
        final PageBean<PopularText> popularTextPageBean = popularTextService.getPage(popularTextQuery);
        final PageBean<PopularTextVO> pageBean = popularTextConverter.entityPageBean2voPageBean(popularTextPageBean);
        return ResultBean.success(pageBean);
    }

    @GetMapping("/v1/details/{id}")
    public ResultBean<PopularTextDetails> getDetails(@PathVariable("id") Integer id) {
        final PopularText popularText = popularTextService.getById(id);
        final PopularTextDetails popularTextDetails = popularTextConverter.popularText2popularTextDetails(popularText);
        return ResultBean.success(popularTextDetails);
    }

    @PutMapping("/v1/update")
    public ResultBean<Void> update(@RequestBody PopularTextDTO popularTextDTO) {
        final PopularText popularText = popularTextConverter.dto2entity(popularTextDTO);
        popularTextService.insert(popularText);
        return ResultBean.success("修改成功!", null);
    }

    @PostMapping("/v1/insert")
    public ResultBean<Void> insert(@RequestBody PopularTextDTO popularTextDTO) {
        final PopularText popularText = popularTextConverter.dto2entity(popularTextDTO);
        popularTextService.update(popularText);
        return ResultBean.success("添加成功!", null);
    }

    @DeleteMapping("/v1/delete/{id}")
    public ResultBean<Void> delete(@PathVariable("id") Integer id) {
        popularTextService.deleteById(id);
        return ResultBean.success("删除成功!", null);
    }

    //3,5,88,8
    @DeleteMapping("/v1/batchDelete/{ids}")
    public ResultBean<Void> batchDelete(@PathVariable("ids") String ids) {
        popularTextService.deleteByIds(ids);
        return ResultBean.success("批量删除成功!", null);
    }

}
